package com.mrjy.common.config;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;;
/**
 * 首先要继承 ServerEndpointConfig,并实现 modifyHandshake方法，该方法有个HandshakeRequest参数
 * 这时可以在实现ServerEndPoint的类中作为configurator参数
 * @author Administrator
 *
 */
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator{

	@Override
	public void modifyHandshake(ServerEndpointConfig sec,
			HandshakeRequest request, HandshakeResponse response) {
//		super.modifyHandshake(sec, request, response);
		HttpSession httpSession = (HttpSession)request.getHttpSession();
		if (httpSession==null) {
			HttpSession JSESSIONID = WebSocket.getOnlineUser(request.getQueryString());
			if (JSESSIONID != null) {
				sec.getUserProperties().put(HttpSession.class.getName(),JSESSIONID);
			}
		}else {
			sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
		}
	}
	
}
